<?php
namespace app\feedback\controller;
use think\Controller;
use think\Db;

/**
 *
 */
class Feedback extends Logincheck{

    function modify($bid='1'){
        //返回新增审核表所有数据
        $res = db('review')->where('bangdan_id',$bid)->order('mark asc,addtime desc')->select();
        foreach ($res as $k => $item) {
            // $item['teacase_id']
            $res[$k]['nickname'] = find_admin_user($res[$k]['review_id']);
            $area = db('case_area')->alias('c_a')->where('teacase_id',$item['teacase_id'])->join('teaarea area','c_a.teaarea_id=area.id','left')->field('area aname,area.id aid')->select();
            $res[$k]['area'] = $area;
        }
        // mp($res);
        //返回茶类
        $retvals = getTeaCase();
        $this->assign('teacase', $retvals);
        //返回产区
        $retval = getTeaArea();
        $this->assign('teaarea', $retval);
        // print_pre($res);
        $this->assign("bid", $bid);
        $this->assign("res", $res);
        // mp($res);
        return $this->fetch();
    }
    function updatemodify($bid='1'){
        //返回所有修改审核表数据
        $res = db('review_exist')->where('bangdan_id',$bid)->order('updatetime desc')->select();
        foreach ($res as $k => $item) {
            // $item['teacase_id']
            $res[$k]['nickname'] = find_admin_user($res[$k]['review_id']);
            $area = db('case_area')->alias('c_a')->where('teacase_id',$item['teacase_id'])->join('teaarea area','c_a.teaarea_id=area.id','left')->field('area aname,area.id aid')->select();
            $res[$k]['area'] = $area;
        }
        //返回茶类
        $retvals = getTeaCase();
        $this->assign('teacase', $retvals);
        //返回产区
        $retval = getTeaArea();
        $this->assign('teaarea', $retval);
        // print_pre($res);
        $this->assign("bid", $bid);
        $this->assign("res", $res);
        // mp($res);
        return $this->fetch();
    }
    // modify新增审核通过接口
    function modifysuccess($id){
        $user = session('user');
        if(!$id)return 0;
        $data = [
            'mark'=>1,
            'review_id' => $user['id']
        ];
        db('review')->where('Id',$id)->update($data);
        return 1;
    }

    // modify新增审核不通过接口
    function modifyfialed($id){
        $user = session('user');
        if(!$id)return 0;
        $data = [
            'mark'=>2,
            'review_id' => $user['id']
        ];
        db('review')->where('Id',$id)->update($data);
        return 1;
    }

    // modify修改审核通过接口
    function updatemodifysuccess($id){
        $user = session('user');
        if(!$id)return 0;
        $data = [
            'mark'=>1,
            'review_id' => $user['id']
        ];
        db('review_exist')->where('Id',$id)->update($data);
        $res = db('review_exist')->where('Id',$id)->select();
        return 1;
    }

    // modify修改审核不通过接口
    function updatemodifyfialed($id){
        $user = session('user');
        if(!$id)return 0;
        $data = [
            'mark'=>2,
            'review_id' => $user['id']
        ];
        db('review_exist')->where('Id',$id)->update($data);
        $res = db('review_exist')->where('Id',$id)->select();
        return 1;
    }

     //返回某茶类对应的产地JSON数据
    function reTeaAreaJson($teacase_id)
    {
        // $data = $this->reTeaArea($teacase_id);
        $data = $this->reTeaArea($teacase_id);
        return json_encode($data);
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
        $retval = db("case_area")->field("teaarea_id")->where("teacase_id",$teacase_id)->select();
        $areas = getTeaArea();
        $data = [];
        foreach ($retval as $ret) {
            foreach ($areas as $area) {
                if($ret["teaarea_id"]==$area["id"]){
                    $data[] = $area;
               }
           }
       }
       return $data;
    }
    //展示评论
    function comment()
    {
        $retval = model('Feedback')->comment();
        // print_pre($retval);
        $this->assign('data',$retval);
        return $this->fetch();
    }

    //展示合作提出方
    function cooperation()
    {
        $retval = model('Feedback')->cooperation();
        // print_pre($retval);
        $this->assign('data',$retval);
        return $this->fetch();

    }

    function updatacomment($id){
        if($id){
            db('comment')->where('id',$id)->update(['status'=>1]);
            return 1;
        }else{
            return 0;
        }
    }
    function updatacooperation($id){
        if($id){
            db('cooperation')->where('id',$id)->update(['status'=>1]);
            return 1;
        }else{
            return 0;
        }
    }

    //增加匠人信息
    function addjr($name, $teacase_id, $teaarea_id, $bangdan_id, $briefintro, $contact, $phone, $wechat, $detailintro)
    {
        //工作年限得分
        $seniority = seniority($detailintro);

        //荣誉得分
        $honor_point = honorPoint($briefintro);
        if($honor_point==0){
            $honor_point = honorPoint($detailintro);
        }

        //社会认可度得分
        $recognition_point = recognition($briefintro);
        if($recognition_point==0){
            $recognition_point = recognition($detailintro);
        }

        //社会身份得分
        $socialIdentity = socialIdentity($briefintro);
        if($socialIdentity ==0){
            $socialIdentity = socialIdentity($detailintro);
        }

        $score = $seniority + $honor_point + $recognition_point + $socialIdentity;

        $data = [
            "name" => $name,
            "score" => $score,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "briefintro" => $briefintro,
            "contact" => $contact,
            "phone" => $phone, 
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "addtime" => time()
        ];
        $t = [
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        $ts = db('Jren')->where($t)->select();
        if(count($ts)>0){
            return $this->error("已存在数据，入库失败");
        }
        // mp($data);
        db('Jren')->insert($data);
        return $this->success("入库成功");
    }

    //增加品牌信息
    function addpp($name, $relatedwords, $teacase_id, $teaarea_id, $bangdan_id, $sale, $company, $pnumber, $contact, $phone, $wechat, $detailintro)
    {
        $established_point = established($detailintro);
        $sale_point = sale($sale);
        $pnumber_point = pnumber($pnumber);
        $score = $established_point + $sale_point +$pnumber_point;

        $data = [
            "name" => $name,
            "relatedwords" => $relatedwords,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "sale" => $sale,
            "company" => $company,
            "pnumber" => $pnumber,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "score" => $score,
            "addtime" => time()
        ];
        $t = [
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        $ts = db('Pinpai')->where($t)->select();
        if(count($ts)>0){
            return $this->error("已存在数据，入库失败");
        }
        // mp($data);
       db('Pinpai')->insert($data);
        return $this->success("入库成功");

    }

    // 添加珍品信息
     function addzp($teacase_id, $teaarea_id, $bangdan_id, $name, $producer, $zs='×', $gs='×', $ct='×', $ds='×', $orther="", $price, $contact, $phone, $wechat, $detailintro)
    {
        $score = evaluate($detailintro);
        $data = [
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "name" => $name,
            "producer" => $producer,
            "zs" => $zs,
            "gs" => $gs,
            "ct" => $ct,
            "ds" => $ds,
            "other" => $orther,
            "price" => $price,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "score" => $score,
            "addtime" => time()
        ];
        $t = [
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        $ts = db('Zhenpin')->where($t)->select();
        if(count($ts)>0){
            return $this->error("已存在数据，入库失败");
        }
        db('Zhenpin')->insert($data);
        return $this->success("入库成功");

    }
/*     修改申请审核通过入库        */
    //修改匠人信息
    function updatejr($mark,$id,$exist_id, $teacase_id, $teaarea_id, $bangdan_id, $briefintro, $contact, $phone, $wechat,$detailintro)
    {
        //工作年限得分
        $seniority = seniority($detailintro);
        $retval = 0;
        //荣誉得分
        $honor_point = honorPoint($briefintro);
        if($honor_point==0){
            $honor_point = honorPoint($detailintro);
        }

        //社会认可度得分
        $recognition_point = recognition($briefintro);
        if($recognition_point==0){
            $recognition_point = recognition($detailintro);
        }

        //社会身份得分
        $socialIdentity = socialIdentity($briefintro);
        if($socialIdentity ==0){
            $socialIdentity = socialIdentity($detailintro);
        }

        $score = $seniority + $honor_point + $recognition_point + $socialIdentity;

        $data = [
            'exist_id'=>$exist_id,
            "score" => $score,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "briefintro" => $briefintro,
            "contact" => $contact,
            "phone" => $phone, 
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "updatetime" => time()
        ];
        $t = [
            'id'=>$id,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        if($mark==1){
           $ts = db('jiangren')->where($t)->select();
            if(count($ts)<0){
                return $this->error('不存在数据，修改失败');
            }
            // mp($data);
            $retval = model('Feedback')->uodateintsql($data); 
        }
        
        if($retval == 1){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }
    }

    //修改品牌信息
    function updatepp($mark,$id,$exist_id, $relatedwords, $teacase_id, $teaarea_id, $bangdan_id, $sale, $company, $pnumber, $contact, $phone, $wechat, $detailintro)
    {
        $retval = 0;
        $established_point = established($detailintro);
        $sale_point = sale($sale);
        $pnumber_point = pnumber($pnumber);
        $score = $established_point + $sale_point +$pnumber_point;
        $data = [
            'exist_id'=>$exist_id,
            "relatedwords" => $relatedwords,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "sale" => $sale,
            "company" => $company,
            "pnumber" => $pnumber,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "score" => $score,
            "updatetime" => time()
        ];
        $t = [
            'id'=>$id,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        if($mark==1){
            $ts = db('Pinpai')->where($t)->select();
            if(count($ts)<0){
                return $this->error('不存在数据，修改失败');
            }
            // mp($data);
            $retval = model('Feedback')->uodateintsql($data);
        }
        if($retval == 1){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }

    }

    // 修改珍品信息
     function updatezp($mark,$id,$exist_id, $teacase_id, $teaarea_id, $bangdan_id,$producer, $zs='×', $gs='×', $ct='×', $ds='×', $orther="", $price, $contact, $phone, $wechat, $detailintro)
    {
        $retval = 0;
        $score = evaluate($detailintro);
        $data = [
            'exist_id'=>$exist_id,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id,
            "producer" => $producer,
            "zs" => $zs,
            "gs" => $gs,
            "ct" => $ct,
            "ds" => $ds,
            "other" => $orther,
            "price" => $price,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "score" => $score,
            "updatetime" => time()
        ];
        $t = [
            'id'=>$id,
            "teacase_id" => $teacase_id,
            "teaarea_id" => $teaarea_id,
            "bangdan_id" => $bangdan_id
        ];
        if($mark==1){
            $ts = db('Zhenpin')->where($t)->select();
            if(count($ts)<0){
                return $this->error('不存在数据，修改失败');
            }
            $retval = model('Feedback')->uodateintsql($data);
        }
        if($retval == 1){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }

    }







}
?>